﻿using Aishow.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Aishow.Website
{
    /// <summary>
    /// 图片验证码处理程序
    /// </summary>
    public class CaptchaHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            // 生成验证码
            CaptchaGenerator captchaGenerator = new CaptchaGenerator();
            string code = captchaGenerator.Text;
            Bitmap captcha = captchaGenerator.Image;
            // 保存验证码到Session
            context.Session.Add("Captcha", code);
            // 输出验证码图片
            context.Response.ContentType = "image/jpeg";
            captcha.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}